//
//  YCDetailViewController.swift
//  GZWeibo007
//
//  Created by 姚伟聪 on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD

class YCDetailViewController: UIViewController,UITableViewDelegate, UITableViewDataSource,CZStatusCellDelegate ,HbottomViewDelegate,YCHeadViewDelegate{
    
    //MARK: - 属性
    var status:CZStatus?
    var commentModel: [YSPCommentModel]?{
        
        didSet{
        
            detailTableView.reloadData()
        
        }
    
    }
    //评论按钮点击标志
    var commentBtnFlag:Bool = false
    
    
    override func loadView() {
        super.loadView()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        paperUI()
        self.title = "微博详情"
       loadCommentData()
        headView.delegate = self
        
    }
   
    //加载评论数据
    func  loadCommentData(){
        
        let since_id = commentModel?.first?.id ?? 0
        let max_id = 0
    
        YSPCommentModel.getNewComment(since_id, max_id: max_id, id: (status?.id)!) { (result, error) -> () in
            
            guard error == nil else{
                return
            }
            
            let count = result?.count
            if count == 0 {
                return
            }
            
            if since_id > 0 {
                self.commentModel = result! + self.commentModel!
            }
            else {
                self.commentModel = result
                print("\(self.commentModel?.count)")
            }
        }
    }
    
    func loadMoreCommentData() {
        let since_id = 0
        let max_id = commentModel?.last?.id ?? 0
        
        detailTableView.infiniteScrollingView.stopAnimating()
        YSPCommentModel.getNewComment(since_id, max_id: max_id, id: (status?.id)!) { (result, error) -> () in
            
            guard error == nil else{
                return
            }
            
            let count = result?.count
            if count == 0 {
                return
            }
            
            if max_id > 0 {
                self.commentModel = self.commentModel! + result!
            }
        }
    }
    
    private func paperUI(){
        self.view.addSubview(detailTableView)
        self.view.addSubview(hbV)
        
        detailTableView.addInfiniteScrollingWithActionHandler { () -> Void in
            self.loadMoreCommentData()
        }
        
        hbV.translatesAutoresizingMaskIntoConstraints = false
        detailTableView.translatesAutoresizingMaskIntoConstraints = false
        
       self.view.addConstraint(NSLayoutConstraint(item: detailTableView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: detailTableView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -44))
        self.view.addConstraint(NSLayoutConstraint(item: detailTableView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
         self.view.addConstraint(NSLayoutConstraint(item: detailTableView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        
        self.view.addConstraint(NSLayoutConstraint(item: hbV, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: hbV, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.view.addConstraint(NSLayoutConstraint(item: hbV, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:UIScreen.mainScreen().bounds.width))
        self.view.addConstraint(NSLayoutConstraint(item: hbV, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:44))
        
        
    }
    
    //MARK: - tableView代理和数据源方法
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 2
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0{
            return 1
        }
        return commentModel?.count ?? 0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
        if indexPath.section == 0{
            
        let cellID = status!.cellID()
        let cell = tableView.dequeueReusableCellWithIdentifier(cellID) as! CZStatusCell
        cell.status = status
         //一定要先计算行高
        let rowHeight = cell.rowHeight(status!)
        cell.status?.rowHeight = rowHeight
            
        cell.delegate = self
        return cell
     }else{
        
                let cell = tableView.dequeueReusableCellWithIdentifier("commentCell") as! CZCommetCell
                cell.commentModel = commentModel?[indexPath.row]
                return cell
          
            
           
            
        }
    }
    //设置cell高度
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        if indexPath.section == 0{
          return (status?.rowHeight)!
        }
        
        let comment = commentModel![indexPath.row]
        
        print("外面的\(comment.text)")
        
        let cell = tableView.dequeueReusableCellWithIdentifier("commentCell") as! CZCommetCell
        
        let rowHight = cell.rowHeight(comment)
        
        print("rowHight:\(rowHight)")
        return rowHight
        
        
    }
    //设置头部view
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if section == 1{
            return headView
        }
        
        return nil
    }
    //设置高度
    func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if section == 1{
            return 44
        }
        return 0
    }
    
    
    //MARK: - 代理回调
    func forwardCellClick(status: CZStatus) {
        
        jumpController(status)
    }
    //评论按钮代理回调
    func commentBtnClick() {
        loadCommentData()
        
    }
    
    //MARK: - 跳转控制器
    private func jumpController(status: CZStatus){
        
        //要跳转的控制器
        let detailViewController = YCDetailViewController()
        //隐藏下面的tarBar
        detailViewController.hidesBottomBarWhenPushed = true
        detailViewController.status = status        //push跳转
        navigationController?.pushViewController(detailViewController, animated: true)
        
    }

    //MARK: - bottom代理方法
    //点击事件
    func commentClick() {
        let Nav = UINavigationController(rootViewController: commentViewC)
        commentViewC.status = status
        self.presentViewController(Nav, animated: true, completion: nil)
    }
    func goodClick() {
        SVProgressHUD.showInfoWithStatus("就他妈没做")
        
    }
    func forwardClick() {
        let Nav = UINavigationController(rootViewController: forwardViewC)
        forwardViewC.status = status
        self.presentViewController(Nav, animated: true, completion: nil)
    }
    
    //MARK: - 懒加载
    //tableView
    private lazy var detailTableView: UITableView = {
    
        let table = UITableView()
        
        table.dataSource = self
        table.delegate = self
        table.estimatedRowHeight = 200
      
        // 去掉cell的分割线
        table.separatorStyle = UITableViewCellSeparatorStyle.None
        
        // 注册原创cell
        table.registerClass(CZStatusNormalCell.self, forCellReuseIdentifier: CZStatusCellReuseIdentifier.StatusNormalCell.rawValue)
        // 注册转发cell
        table.registerClass(CZStatusForwardCell.self, forCellReuseIdentifier: CZStatusCellReuseIdentifier.StatusForwardCell.rawValue)
       
        // 注册评论cell
        table.registerClass(CZCommetCell.self, forCellReuseIdentifier: "commentCell")
        
        
        return table
    
    }()
    
    private lazy var headView : YCHeadView = YCHeadView()
    
    lazy var hbV : HbottomView = {
        let bottom = HbottomView()
        bottom.delegate = self
        return bottom
    
    }()
    

    
   
    
    //
    lazy var commentViewC : HCommentViewController = HCommentViewController()
    lazy var forwardViewC : HForwardViewController = HForwardViewController()
    
}


